package base

import (
	"github.com/gin-gonic/gin"
	"go-api-template/api/http"
	"go-api-template/internal/e"
	"go-api-template/internal/util/stringUtil"
	"go-api-template/service"
)

func (req *CreateSortLinkReq) Request(ctx *gin.Context, s *service.Service) (http.Resp, error) {
	return req.Do(ctx, s)
}

func (req *LoginReq) Request(ctx *gin.Context, s *service.Service) (resp http.Resp, err error) {
	switch req.Mode {
	case LoginReqModeAccount:
		if req.Username == "" || req.Password == "" {
			resp.SetCode(e.ParameterCheckFailed)
			return
		}
	case LoginReqModePhone:
		if req.Phone == "" || req.Password == "" {
			resp.SetCode(e.ParameterCheckFailed)
			return
		}
	case LoginReqModePhoneCaptcha:
		if req.Phone == "" || req.Captcha == "" {
			resp.SetCode(e.ParameterCheckFailed)
			return
		}
	case LoginReqModeEmailCaptcha:
		if req.Email == "" || req.Captcha == "" || stringUtil.CheckEmail(req.Email) {
			resp.SetCode(e.ParameterCheckFailed)
			return
		}
	default:
		resp.SetCode(e.RequestIllegalAccess)
		return
	}

	resp, err = req.Do(ctx, s)
	return
}

func (req *CurrentReq) Request(ctx *gin.Context, s *service.Service) (resp http.Resp, err error) {
	if req.UserId == "" {
		req.UserId = ctx.GetString("user_id")
	}
	return req.Do(ctx, s)
}

func (req *SendCaptchaReq) Request(ctx *gin.Context, s *service.Service) (resp http.Resp, err error) {
	switch req.Mode {
	case SendCaptchaReqModePhone:
		if req.Phone == "" {
			resp.SetCode(e.ParameterCheckFailed)
			return
		}
	case SendCaptchaReqModeEmail:
		if req.Email == "" {
			resp.SetCode(e.ParameterCheckFailed)
			return
		}
	}
	return req.Do(ctx, s)
}

func (req *StatusReq) Request(ctx *gin.Context, s *service.Service) (http.Resp, error) {
	return req.Do(ctx, s)
}
